package com.jims.api.v1.controller.sys;

import com.jims.master.common.mapper.JsonMapper;
import com.jims.master.common.utils.CommonUtil;
import com.jims.master.modules.la.model.*;
import com.jims.master.modules.la.service.LaUserService;
import com.thoughtworks.xstream.XStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by think on 2016/4/27.
 */
@RestController
@RequestMapping(value = "rest/lahealth")
public class HealthDataApi {

    /**
     * 数据类型（user:用户信息 check:血压心率体温 bp:血压 ecg:心电监测 water:水分/油分 fat:身体指数 sugar:血糖 breath:呼吸率 pressure:抵抗力、情绪、疲劳数据）
     */
    public static final String DATATYPE_USER = "user";
    public static final String DATATYPE_CHECK = "check";
    public static final String DATATYPE_BP = "bp";
    public static final String DATATYPE_ECG = "ecg";
    public static final String DATATYPE_WATER = "water";
    public static final String DATATYPE_FAT = "fat";
    public static final String DATATYPE_SUGAR = "sugar";
    public static final String DATATYPE_BREATH = "breath";
    public static final String DATATYPE_PRESSURE = "pressure";
    /**
     * 结果状态（ -1：验证失败   0：插入数据失败   1：插入数据成功  2:数据格式错误  ）
     */
    public static final String RESULT_UNAUTH = "-1";
    public static final String RESULT_FAIL = "0";
    public static final String RESULT_SUCCESS = "1";
    public static final String RESULT_DATAERROR = "2";

    //注入service
    @Autowired
    LaUserService laUserService;

    /**
     * @param jsonData json格式数据对象
     * @param dataType 数据类型（user:用户信息 check:血压心率体温 bp:血压 ecg:心电监测 water:水分/油分 fat:身体指数 sugar:血糖 breath:呼吸率 pressure:抵抗力、情绪、疲劳数据）
     * @param psw
     * @return String 返回类型  -1：验证失败   0：插入数据失败   1：插入数据成功  2:数据格式错误
     */
    @RequestMapping(value = "syncData")
    public String syncData(String jsonData, String dataType,String psw) {
        String reslut = RESULT_FAIL;
        if (psw != null && !"".equals(psw)) {
            String[] str = psw.split("@");
            if (CommonUtil.getValue("la_username").equals(str[0]) && CommonUtil.getValue("la_password").equals(str[1])){
                switch (dataType){
                    case DATATYPE_USER:
                        User user = (User) JsonMapper.fromJsonString(jsonData, User.class);
                        reslut = laUserService.saveUser(user);
                        break;
                    case DATATYPE_CHECK:
                        HealthCheckRecord healthCheckRecord = (HealthCheckRecord)JsonMapper.fromJsonString(jsonData,HealthCheckRecord.class);
                        reslut = laUserService.saveHealthCheckRecord(healthCheckRecord);
                        break;
                    case DATATYPE_BP:
                        BpRecordWithBLOBs bpRecord = (BpRecordWithBLOBs) JsonMapper.fromJsonString(jsonData, BpRecordWithBLOBs.class);
                        reslut = laUserService.saveBpRecord(bpRecord);
                        break;
                    case DATATYPE_ECG:
                        EcgRecord ecgRecord = (EcgRecord) JsonMapper.fromJsonString(jsonData, EcgRecord.class);
                        reslut = laUserService.saveEcgRecord(ecgRecord);
                        break;
                    case DATATYPE_WATER:
                        WaterOilRecord waterOilRecord = (WaterOilRecord) JsonMapper.fromJsonString(jsonData, WaterOilRecord.class);
                        reslut = laUserService.saveWaterRecord(waterOilRecord);
                        break;
                    case DATATYPE_FAT:
                        FatRecord fatRecord = (FatRecord) JsonMapper.fromJsonString(jsonData, FatRecord.class);
                        reslut = laUserService.saveFatRecord(fatRecord);
                        break;
                    case DATATYPE_SUGAR:
                        SugarRecord sugarRecord = (SugarRecord) JsonMapper.fromJsonString(jsonData, SugarRecord.class);
                        reslut = laUserService.saveSugarRecord(sugarRecord);
                        break;
                    case DATATYPE_BREATH:
                        BreathFreq breathFreq = (BreathFreq) JsonMapper.fromJsonString(jsonData, BreathFreq.class);
                        reslut = laUserService.saveBreathFreq(breathFreq);
                        break;
                    case DATATYPE_PRESSURE:
                        PressureTestRecord pressureTestRecord = (PressureTestRecord) JsonMapper.fromJsonString(jsonData, PressureTestRecord.class);
                        reslut = laUserService.savePressureRecord(pressureTestRecord);
                        break;
                    default: reslut = RESULT_DATAERROR;
                }
            }
            else{
                reslut = RESULT_UNAUTH;
            }
        }
        else{
            reslut = RESULT_UNAUTH;
        }

        return reslut;
    }

    public static void main(String[] args) {
        XStream xstream = new XStream();
        User user = new User();
        user.setUserId("00000000001");
        user.setLoginName("18888888888");
        user.setUserName("席华龙");
        user.setUserType(1);
         String st = xstream.toXML(user);
        System.out.println(st);
        String juser = JsonMapper.toJsonString(user);
        User user2 = (User) JsonMapper.fromJsonString(juser,User.class);
        System.out.println(111+juser);
    }

}
